<?php

namespace app\rongcui\controller;

use app\rongcui\model\RongcuiFilesModel;
use cmf\controller\AdminBaseController;

class FilesController extends AdminBaseController
{
    public function index() {
        $where = [
            'files.is_del' => 0,
            'files.type' => ['elt',1]
        ];
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname|files.file_name|customer.areas'] = ['like', "%$keyword%"];
        }
        if (input('type',0,'intval')) {
            $type = input('type',0,'intval');
            $where['customer.type'] = $type;
        }
        $rongcuiFilesModel = new RongcuiFilesModel();
        $lists = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','user.id = files.uid')
            ->join('__RONGCUI_POTENTIAL_CUSTOMER__ customer','customer.id = files.pid and files.type = 1 and customer.type <> 0 and customer.abutment <= 1')
            ->join('__CURRENCY__ currency','currency.id = customer.currency','left')
            ->field('files.*,user.user_nickname,customer.areas,customer.selling_price,registered_capital,currency.name as cname,abutment,customer.type')
            ->where($where)
            ->order('files.id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);

        $this->assign('where_r',$where_r);
        $this->assign('lists',$lists);
        $this->assign('page',$lists->render());
        return $this->fetch();
    }

//    public function add() {
//        if($this->request->isPost()) {
//            $arrData = $this->request->param();
//            $arrData['create_time'] = time();
//            $arrData['uid'] = cmf_get_current_admin_id();
//            db('files')->insert($arrData);
//            $this->success('添加成功',cmf_url('index'));
//        }
//        return $this->fetch();
//    }

    public function del() {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('参数错误！');
        }
        $rongcuiFilesModel = new RongcuiFilesModel();
        $rongcuiFilesModel->where([
            'id' => $id
        ])->setField('is_del',1);
        $this->success('删除成功！');
    }
}